package org.toools.gwt.demo.jpa;

import java.io.Serializable;
import java.util.Collection;
import java.util.Vector;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import net.sf.gilead.pojo.java5.LightEntity;

/**
 *
 * @author stefan asseg
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Company.getAll", query = "SELECT c FROM Company c"),
    @NamedQuery(name = "Company.getById", query = "SELECT c FROM Company c WHERE c.id = :id"),
    @NamedQuery(name = "Company.getByName", query = "SELECT c FROM Company c WHERE c.name = :name")
})
public class Company extends LightEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;
    private String name;
    @ManyToOne(cascade = CascadeType.ALL)
    private Account account;
    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
    private Collection<CompanyPart> companyParts = new Vector<CompanyPart>();

    public Company() {
    }

    public Company(String name, Account account) {
        this.name = name;
        this.account = account;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public Collection<CompanyPart> getCompanyParts() {
        return companyParts;
    }

    public void setCompanyParts(Collection<CompanyPart> companyParts) {
        this.companyParts = companyParts;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Company)) {
            return false;
        }
        Company other = (Company) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Company [id = " + id + "] [name = " + name + "] [account = " + account + "]";
    }
}
